#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <arpa/inet.h>

#define PORT 8080
#define BUFFER_SIZE 1024

int main(){
    int sock=0;
    struct sockaddr_in serv_addr;
    char buffer[BUFFER_SIZE]={0};
    const char *hello="Hello from client";
    //创建socket
    if((sock=socket(AF_INET, SOCK_STREAM, 0))<0){
        perror("socket creation failed");
        return -1;
    }
    serv_addr.sin_family=AF_INET;
    serv_addr.sin_port=htons(PORT);

    //将IP地址转换为网络字节序
    if(inet_pton(AF_INET, "127.0.0.1",&serv_addr.sin_addr)<=0){
       printf("\nInvalid address/ Address not supported \n");
       return -1;
    }

    //连接服务器
    if(connect(sock,(struct sockaddr *)&serv_addr, sizeof(serv_addr))<0){
        perror("connect failed");
        return -1;
    }

    //向服务器发送数据
    send(sock, hello, strlen(hello), 0);
    printf("Hello message sent\n");

    //读取服务器发送的数据
    read(sock, buffer, BUFFER_SIZE);
    printf("服务器发送的数据:%s\n", buffer);
    close(sock);
    return 0;
}